package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @program: Spring_day01_01_IOC入门程序
 * @description: 功能：实现查询所需的时间
 * @author: xuejingjing
 * @create: 2021-08-03 20:05
 **/


@Component
@Aspect  //这个类是个切面类，里面的方法做通知功能
public class RunTimeMonitorAdvice {

    //定义一个切入点，监控业务层接口
    @Pointcut("execution(* com.itheima.service.*Service.find*(..))")
    public void pt(){

    }

    //通知方法

    //环绕通知
    @Around("pt()")//切入点  //这个参数是干啥的？
    public  Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
/**
 * 这里没看懂
 */
        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();
        long sum = 0L;

        for (int i = 0; i < 10000 ; i++) {
            long startTime = System.currentTimeMillis();

            pjp.proceed(pjp.getArgs());

            long endTime = System.currentTimeMillis();

            sum += endTime-startTime;

        }

        System.out.println("className:"+className+"(万次)"+sum);

        return null;

    }
}
